package com.scpii.universal.struts.manage;

import java.io.File;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang.xwork.StringUtils;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.doone.comm.paginaction.Pagination;
import com.scpii.universal.action.BaseAction;
import com.scpii.universal.constant.GenericConstants;
import com.scpii.universal.dto.EnterpriseDto;
import com.scpii.universal.hibernate.entity.EnterpriseTab;
import com.scpii.universal.hibernate.entity.TypeTab;
import com.scpii.universal.service.EnterpriseManageService;
import com.scpii.universal.service.EnterpriseService;
import com.scpii.universal.service.TypeService;
import com.scpii.universal.util.ImgAndFilePath;
import com.scpii.universal.util.StreamUtil;

@Controller
@Scope("prototype")
public class EnterManageAction extends BaseAction {

	/**
	 * serialVersionUID
	 */
	private static final long serialVersionUID = -7141789534482490652L;
	@Resource
	private EnterpriseManageService enterpriseManageService;
	@Resource
	private EnterpriseService enterpriseService;
	@Resource
	private TypeService typeService;
	// 分页信息
	private Pagination page = new Pagination();
	// 企业信息Dto
	private EnterpriseDto enterpriseDto;
	// 查询dto
	private EnterpriseDto condition;
	// 企业信息的主键
	private Integer intEnterpId;
	// 上传的文件流
	private File myFile;
	// 上传文件的文件名称
	private String myFileFileName;
	// 企业行业信息
	private List<TypeTab> typeTabList;

	private List<EnterpriseDto> enterpriseList;

	/**
	 * 
	 * @return
	 * @author zhangqiaing 2012-7-17下午02:36:13
	 */
	public String goEnterprise() {
		// 获取企业分类信息
		typeTabList = typeService.hqlGetList(
				" from TypeTab t where t.intParentTypeId=17", null);
		request.setAttribute("typeTabList", typeTabList);
         if(condition==null){
        	 condition=new EnterpriseDto();
         }
         condition.setIntEnterpId(getUserInfo().getIntEnterpId());
		// 判断第一次进入分页信息
		if (getSelectPerPage("page") == -1) {
			page.setPageRecord(GenericConstants.PAGE_NO);
		} else {
			page.setPageRecord(getSelectPerPage("page"));
		}
		// 判断是否是第一次查询
		if (getQueryFlag("page") == -1) {
			page.setTotalRecord(enterpriseManageService.getCountById(condition));
		} else {
			page.setTotalRecord(getQueryFlag("page"));
		}
		setPageParameters(page, "page");
		enterpriseList = enterpriseManageService.getEnterpriceList(condition,
				page.getCurrentPage(), page.getPageRecord());

		return SUCCESS;
	}

	/**
	 * 根据ID获取企业信息 用于编辑
	 * 
	 * @return
	 * @author zhangqiaing 2012-7-13上午11:13:04
	 */
	public String goEditEnterprise() {
		enterpriseDto = enterpriseManageService.getEnterpriceById(intEnterpId);
		return SUCCESS;
	}

	/**
	 * 保存企业编辑的信息
	 * 
	 * @return
	 * @author zhangqiaing 2012-7-13下午04:27:09
	 */
	public String doEditEnterprise() {

		String filePath = "";
		if (myFile != null) {
			// 创建图片资源目录
			ImgAndFilePath.createWorkPic(getWebAppsRealPath(), getUserInfo()
					.getIntEnterpId(), getUserInfo().getAppId());
			// 文件上櫥绝对路径
			String imgAbsolutePath = ImgAndFilePath.ENTERP_WORK_DIR + "_"
					+ getUserInfo().getIntEnterpId() + File.separator;

			StreamUtil.uploadActiveImgs(myFile, getWebAppsRealPath()
					+ imgAbsolutePath + myFileFileName);
			filePath = getServerURL() + imgAbsolutePath
					+ myFileFileName;
		}
		// 文件保存成功 开始更改数据库
		EnterpriseTab enterTab = enterpriseService.get(enterpriseDto
				.getIntEnterpId());
		if (myFile != null) {
			// 删除以前的图片
			if (!StringUtils.isBlank(enterTab.getVarEnterpLicenseImgUrl())){
				File newFile = new File(enterTab.getVarEnterpLicenseImgUrl());
				if (newFile.exists()) {
					newFile.delete();
				}
			}
			// 文件路劲
			enterTab.setVarEnterpLicenseImgUrl(filePath.replace("\\", "/"));
		}

		enterTab.setVarEnterpName(enterpriseDto.getVarEnterpName());
		enterTab.setVarEnterpLegalPerson(enterpriseDto
				.getVarEnterpLegalPerson());
		enterTab.setIntEnterpTradeType(enterpriseDto.getIntTypeId());
		enterTab.setVarEnterpPhonenum(enterpriseDto.getVarEnterpPhonenum());
		enterTab.setVarEnterpEmail(enterpriseDto.getVarEnterpEmail());
		enterTab.setVarEnterpAddress(enterpriseDto.getVarEnterpAddress());
		// update
		enterpriseService.update(enterTab);

		return SUCCESS;
	}

	/**
	 * 删除企业信息
	 * 
	 * @return
	 * @author zhangqiaing 2012-7-17上午09:42:10
	 */
	public String dodelEnterprise() {
		EnterpriseTab enterTab = enterpriseService.get(intEnterpId);

		enterTab.setIntDeleteSign(GenericConstants.DELETE_SIGN_DELETED);
		enterpriseService.update(enterTab);
		return SUCCESS;
	}

	/**
	 * 跳转到企业门店列表
	 * 
	 * @return
	 * @author zhangqiaing 2012-7-17上午10:28:54
	 */
	public String goGetShop() {

		return SUCCESS;

	}

	public EnterpriseDto getCondition() {
		return condition;
	}

	public void setCondition(EnterpriseDto condition) {
		this.condition = condition;
	}

	public File getMyFile() {
		return myFile;
	}

	public void setMyFile(File myFile) {
		this.myFile = myFile;
	}

	public String getMyFileFileName() {
		return myFileFileName;
	}

	public void setMyFileFileName(String myFileFileName) {
		this.myFileFileName = myFileFileName;
	}

	public Integer getIntEnterpId() {
		return intEnterpId;
	}

	public void setIntEnterpId(Integer intEnterpId) {
		this.intEnterpId = intEnterpId;
	}

	public EnterpriseDto getEnterpriseDto() {
		return enterpriseDto;
	}

	public void setEnterpriseDto(EnterpriseDto enterpriseDto) {
		this.enterpriseDto = enterpriseDto;
	}

	public Pagination getPage() {
		return page;
	}

	public void setPage(Pagination page) {
		this.page = page;
	}

	public List<EnterpriseDto> getEnterpriseList() {
		return enterpriseList;
	}

	public void setEnterpriseList(List<EnterpriseDto> enterpriseList) {
		this.enterpriseList = enterpriseList;
	}

}
